package com.service.poi.util;

import cn.hutool.core.io.IoUtil;
import com.service.poi.constant.FileType;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author zhaoj
 * @create 2021/12/22
 */
public class FileReaderUtil {

    /**
     * 读取文件内容
     * @return
     */
    public static String fileRead(String path, String ext) throws IOException {
        FileInputStream fos = new FileInputStream(path);
        return fileRead(fos, ext);
    }


    public static String fileRead(InputStream is, String ext) throws IOException {
        FileType fileType = FileType.enumOf(ext);

        if(fileType.isText()) {
            String result = IoUtil.readUtf8(is);
            return result;
        } else if(fileType.isDoc()) {
            return readDoc(is, fileType);
        }
        return null;
    }

    private static String readDoc(InputStream is, FileType fileType) throws IOException {
        String result = "";
        switch (fileType) {
            case CSV:
                break;
            case DOC:
                HWPFDocument doc = new HWPFDocument(is);
                result = doc.getText().toString();
                break;
            case DOCX:
                XWPFDocument doc07 = new XWPFDocument(is);
                List<XWPFParagraph> paras = doc07.getParagraphs();
                for(XWPFParagraph para: paras) {
                    result += para.getText().toString();
                }
                break;
            case PPT:
                HSLFSlideShow hss = new HSLFSlideShow(is);
                SlideShowExtractor slide = new SlideShowExtractor(hss);
                result = slide.getText();
                break;
            case PPTX:
                XMLSlideShow xss = new XMLSlideShow(is);
                SlideShowExtractor slide07 = new SlideShowExtractor(xss);
                result = slide07.getText();
                break;
            case XLS:
                break;
            case XLSX:
                break;
            case PDF:
                //获取parse对象
                PDFParser parser = new PDFParser(new RandomAccessBuffer(is));
                parser.parse();
                //获取document对象
                PDDocument pdDocument = parser.getPDDocument();
                //获取总页码
                int pages = pdDocument.getNumberOfPages();
                //读取文本内容
                PDFTextStripper stripper = new PDFTextStripper();
                //设置输出顺序（是否排序）
                stripper.setSortByPosition(true);
                stripper.setStartPage(1);
                stripper.setEndPage(pages);

                stripper.setSortByPosition(false);
                result = stripper.getText(pdDocument);

                pdDocument.close();
                break;
        }
        return result;
    }


}
